<?php
class Product_model extends MY_Model {
	private $list_cat;
	public $total_rows;
    function __construct(){
        parent::__construct();
        $this->list_cat = $this->cm->get_all('cat',array('type'=>'product'),'arr');
    }
    
    function get_by_page($cond){
    	$map = array();
    	$map_in = array();
    	if(!empty($cond['cid'])){
    		$map_in = getCatSubId($this->list_cat,$cond['cid']);
    	}else{
    		$map_in = $this->get_cat_all_id();
    	}
    	
    	if(!empty($cond['ft']) && !empty($cond['st'])){
    		$ft = strtotime($cond['ft']);
    		$st = strtotime($cond['st']);
    		$map['p.atime >='] = $ft;
    		$map['p.atime <='] = $st;
    	}elseif(!empty($cond['ft']) && empty($cond['st'])){
    		$ft = strtotime($cond['ft']);
    		$map['p.atime >='] = $ft;
    	}elseif(empty($cond['ft']) && !empty($cond['st'])){
    		$st = strtotime($cond['st']);
    		$map['p.atime <='] = $st;
    	}

    	$this->total_rows = $this->db ->from('product p')
    		->select('p.id')
    		->join('img i','i.id=p.title_img_id','left')
    		->where($map)
    		->where_in('p.cid',$map_in)
    		->count_all_results();
    	
    	$query = $this->db ->from('product p')
    		->select('p.*,i.spath as title_img_spath')
    		->join('img i','i.id=p.title_img_id','left')
    		->where($map)
    		->where_in('p.cid',$map_in)
    		->limit(PAGESIZE,$cond['per_page'])
    		->get();
    	//$list为数组形式的对象集。
    	$list = $query->result();
    	//在每个对象上再加一个属性  cname ：表示当前类的全名称，含它的所有父类，用 > 连接。
    	foreach ($list as $k=>$v){
    		$list[$k]->cname = getCatFname($this->list_cat, $v->cid,' &gt; ');
    	}
    	return $list;
    }
    
    function save_album($pid,$album,$desc,$ord){
    	$arr_data = array();
    	foreach ($album as $k=>$v){
    		$arr_item = array();
    		$arr_item['pid'] = $pid;
    		$arr_item['iid'] = $v;
    		$arr_item['desc'] = $desc[$k];
    		$arr_item['ord'] = $ord[$k];
    		
    		$arr_data[] = $arr_item;
    	}
    	return $this->db->insert_batch('product_img',$arr_data);
    }
    
    function get_by_id($id){
    	
    	$product = parent::get_by_id('product', $id);	
    	$product->title_img = parent::get_by_id('img', $product->title_img_id);
    	$product->album = $this->get_album_by_pid($product->id);
    	return $product;
    }
    
    private function  get_album_by_pid($id){
    	$query = $this->db
    				->from('product_img pi')
    				->select('pi.*,i.*')
    				->join('img i','i.id=pi.iid','left')
    				->where(array('pi.pid'=>$id))
    				->get();
    	$album = $query->result();
    	
    	return $album;
    }

    
    private function get_cat_all_id(){
    	$query = $this->db
    	->from('cat')
    	->select('id')
    	->where('type','product')
    	->get();
    	$list = $query->result();
    	$arr = array();
    	foreach ($list as $v){
    		$arr[] = $v->id;
    	}
    	return $arr;
    }
    
    function del_img_by_product($id){
    	//找出标题图片的id
    	$arr1 = array();
    	$row = $this->cm->get_by_id('product',$id);
    	$arr1[] = $row->title_img_id;
    	//找出相册的ids
    	$arr2 = array();
    	$query= $this->db
    				->from('product_img')
    				->where('pid',$id)
    				->get();
    	$album_list = $query->result();
    	foreach ($album_list as $v){
    		$arr2[] = $v->iid;
    	}
    	
    	$arr_img_id = array();
    	if(count($arr2) < 1){
    		$arr_img_id = $arr1;
    	}else{
    		$arr_img_id = array_merge($arr1,$arr2);
    	}
    	
    	//删除磁盘文件
    	   	

    		$query = $this->db
    		->from('img')
    		->where_in('id',$arr_img_id)
    		->get();
    		$dust = $query->result();
    		foreach($dust as $v){
    			$img = str_replace('system/', '', BASEPATH).$v->path;
    			$simg = str_replace('system/', '', BASEPATH).$v->spath;
    				
    			if(file_exists($img))	unlink($img);
    			if(file_exists($simg))	unlink($simg);
    		}

    	 
    	//删除img表的记录
    	
    		$this->db
    		->from('img')
    		->where_in('id',$arr_img_id)
    		->delete();
    		
    }
	function clear_img(){
		//找出有效图片的id
		$arr1 = array();
		$arr2 = array();
		$valid_img_id = array();
		$list1 = $this->cm->get_all('product');
		foreach($list1 as $v){
			$arr1[] = $v->title_img_id;
		}
		$list2 = $this->cm->get_all('product_img');
		foreach($list2 as $v){
			$arr2[] = $v->iid;
		}
		$valid_img_id = array_merge($arr1,$arr2);
		//找出垃圾图片的id
		$all_img_id = array();
		$unvalid_img_id = array();
		$img = $this->cm->get_all('img');
		foreach ($img as $v){
			$all_img_id[] = $v->id;
		}
		foreach ($all_img_id as $k=>$v){
			if(!in_array($all_img_id[$k], $valid_img_id))
				$unvalid_img_id[] = $all_img_id[$k];
		}

		//删除磁盘垃圾文件,如果有则删除
		$unvalid_num = count($unvalid_img_id);
		
		if($unvalid_num > 0){
			$query = $this->db
			->from('img')
			->where_in('id',$unvalid_img_id)
			->get();
			$dust = $query->result();
			foreach($dust as $v){
				$img = str_replace('system/', '', BASEPATH).$v->path;
				$simg = str_replace('system/', '', BASEPATH).$v->spath;
			
				if(file_exists($img))	unlink($img);
				if(file_exists($simg))	unlink($simg);
			}				
		}

		//删除垃圾记录，如果有则删除		
		if($unvalid_num > 0){
			$this->db
			->from('img')
			->where_in('id',$unvalid_img_id)
			->delete();
			}				
		}
}